//package ca.cbc.msb.mediacms;
//
//import java.net.URISyntaxException;
//import java.net.UnknownHostException;
//import java.util.*;
//
//import ca.cbc.msb.exception.VideoCMSException;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import com.theplatform.authentication.token.api.exception.AuthenticationException;
//import com.theplatform.data.api.exception.ObjectNotFoundException;
//
//import ca.cbc.medialib.service.MediaLibService;
//import ca.cbc.msb.entity.MSBMedia;
//import ca.cbc.msb.entity.MSBRelease;
//import ca.cbc.msb.util.AppConfig;
//
//public class ThePlatformServiceImpl implements MediaCMSService 
//{
//	private static Logger logger = LoggerFactory.getLogger(ThePlatformServiceImpl.class);
//	private MediaLibService mediaLibService;
//
//	@SuppressWarnings("deprecation")
//	public ThePlatformServiceImpl()
//	{
//		try {
//			mediaLibService = new MediaLibService(AppConfig.getProperty("thePlatform.username"),
//												  AppConfig.getProperty("thePlatform.password"),
//												  new String[]{AppConfig.getProperty("thePlatform.account")});
//		} catch (UnknownHostException e) {
//			logger.error("Error when init Medialib...", e);
//		} catch (AuthenticationException e) {
//			logger.error("Error when init Medialib...", e);
//		}
//	}
//	
//	@Override
//	public MSBRelease getRelease(String releaseId) throws VideoCMSException {
//		MSBRelease release = null;	
//		try {
//			Map<String, String> releaseInfos = mediaLibService.getReleaseProperties(releaseId);
//			release = new MSBRelease();
//			release.setReleaseId(releaseId);
//			release.setAirdate(releaseInfos.get("airdate"));
//			release.setDelivery(releaseInfos.get("delivery"));
//			release.setReleaseURL(releaseInfos.get("releaseURL"));
//			release.setUpdatedDate(releaseInfos.get("updatedDate"));
//			release.setYospaceId(releaseInfos.get("yospaceID"));
//			
//			String mediaIdURI = releaseInfos.get("mediaIdURI");
//			release.setMediaId(mediaIdURI.substring(mediaIdURI.lastIndexOf("/")+1));
//            release.setMedia(getMedia(release.getMediaId()));
//		} catch (URISyntaxException e) {
//			logger.error("Error getting Release", e);
//		} catch (ObjectNotFoundException e) {
//			throw new VideoCMSException("Error getting Release", e);
//		}
//
//		return release;
//	}
//
//	@Override
//	public MSBMedia getMedia(String mediaId) throws VideoCMSException {
//		MSBMedia media = null;
//		try {
//			Map<String, String> mediaInfos = mediaLibService.getMediaProperties(mediaId);
//            String [] requiredFields = {"category", "keywords", "thumbnailURL", "adCategory",
//					                    "audioVideo", "backgroundImage", "genre", "liveOndemand", "availableInMobile",
//					                    "relatedClips", "show", "runtime"};
//			MSBMedia.MediaBuilder builder = new MSBMedia.MediaBuilder();
//            builder.withId(mediaId).withTitle(mediaInfos.get("title"))
//                    .withDescription(mediaInfos.get("description"))
//			        .withCategory(mediaInfos.get("category"))
//                    .withKeywords(mediaInfos.get("keywords"))
//                    .withThumbnailURL(mediaInfos.get("thumbnailURL"))
//                    .withAdCategory(mediaInfos.get("adCategory"))
//                    .withAudioVideo(Boolean.valueOf(mediaInfos.get("audioVideo")))
//                    .withBackgroundImage(mediaInfos.get("backgroundImage"))
//                    .withGenre(mediaInfos.get("genre"))
//                    .withLiveOnDemand(Boolean.valueOf(mediaInfos.get("liveOndemand")))
//                    .withAvailableOnMobile(Boolean.valueOf(mediaInfos.get("availableInMobile")))
//                    .withShow(mediaInfos.get("show"))
//                    .withRuntime(mediaInfos.get("timeout"));
//		} catch (URISyntaxException e) {
//			logger.error("Error getting Media", e);
//		} catch (ObjectNotFoundException e) {
//			throw new VideoCMSException("Error getting Media", e);
//		}
//		
//		return media;
//	}
//
//	@Override
//	public void addYospaceId(String releaseId, String yospaceId) throws VideoCMSException {
//		Map<String, String> customInfoMap = new HashMap<String, String>();
//		customInfoMap.put("yospaceID", yospaceId);
//		try {
//			mediaLibService.updateReleaseCustomProperties(releaseId, customInfoMap);
//		} catch (URISyntaxException e) {
//			logger.error("Error updating Yospace",e);
//		} catch (ObjectNotFoundException e) {
//			throw new VideoCMSException("Error getting Media", e);
//		}
//	}
//
//	@Override
//	public  void removeYospaceId(String releaseId) throws VideoCMSException {
//		 //change release Id to null
//		 addYospaceId(releaseId, null);
//			
//		 //set availableInMobile to false
//		 Map<String, String> customInfoMap = new HashMap<String, String>();
//		 customInfoMap.put("availableInMobile", "false");
//		 try {
//			 mediaLibService.updateMediaCustomProperties(getRelease(releaseId).getMediaId(), customInfoMap);
//		 } catch (URISyntaxException e) {
//			 logger.error("Error updating availableInMobile", e);
//		 } catch (ObjectNotFoundException e) {
//			 throw new VideoCMSException("Error updating availableInMobile", e);
//		 }
//	}
//
//	@Override
//	public List<MSBRelease> getMediaReleases(String mediaId) throws VideoCMSException {
//		 List<MSBRelease> releases = new ArrayList<MSBRelease>();
//			
//		try {
//			List<String> releaseIds = mediaLibService.getMediaReleaseIds(mediaId);
//			for(String s : releaseIds)
//				releases.add(this.getRelease(s));
//		} catch (URISyntaxException e) {
//			logger.error("Error when getting releases for media",e);
//		} catch (ObjectNotFoundException e) {
//			throw new VideoCMSException("Error when getting releases for media", e);
//		}
//		
//		if(releases.size() == 0)
//			logger.warn("No release found by given media id!");
//		
//		return releases;
//	}
//
//}
